<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Admin\Model\ArticleModel;
class DeliveryController extends BaseController {
    public function index() {
		$mod = M('goods_delivery');
		$res = $list = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $res = $mod->where($where)->page("$p,$size")->select();
        $count = $mod->where($where)->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
        $this->assign('list', $res);
        $this->assign('page', $page);
        $this->display();
    }
	public function add() {
		if(IS_POST){
			$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Delivery/index');
			$data = I('post.');
			$data['add_time'] = time();
			if(!$data['scope']){
				$this->error("请选择范围", $referurl);
			}else{
				$cont = explode('-',$data['scope']);
				$data['start_scope'] = $cont[0];
				$data['end_scope'] = $cont[1];
			}
			if(!is_numeric($data['price'])){
				$this->error("请输入价格", $referurl);
			}
			$mod = M('goods_delivery');
			$result = $mod->add($data);
			if ($result) {				
				$this->success("添加成功",U('Admin/Delivery/index'));
			} else {
				$this->error("添加失败", $referurl);
			}
		}else{
			$number = M('config')->where(array('name'=>'delivery'))->getField('value');
			$n = ceil($number/1000);
			$data = array();
			for($i=1;$i<$n;$i++){
				if($i==1){
					$data[] = '0-'.($i*1000).'米';
				}else if($i==$n-1){
					$data[] = ($i*1000).'-'.$number.'米';
				}else{
					$data[] = ($i*1000).'-'.(($i+1)*1000).'米';
				}
			}
			$this->assign('info',$data);
        	$this->display();
		}
    }
	public function edit() {
		$mod = M('goods_delivery');
		if(IS_POST){
			$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Delivery/index');
			$data = I('post.');
			$data['add_time'] = time();
			if(!$data['scope']){
				$this->error("请选择范围", $referurl);
			}else{
				$cont = explode('-',$data['scope']);
				$data['start_scope'] = $cont[0];
				$data['end_scope'] = $cont[1];
			}
			if(!is_numeric($data['price'])){
				$this->error("请输入价格", $referurl);
			}
			$mod = M('goods_delivery');
			$result = $mod->where(array('id'=>$data['id']))->save($data);
			if ($result) {
				$this->success("编辑成功",U('Admin/Delivery/index'));
			} else {
				$this->error("编辑失败", $referurl);
			}
		}else{
			$number = M('config')->where(array('name'=>'delivery'))->getField('value');
			$n = ceil($number/1000);
			$data = array();
			for($i=1;$i<$n;$i++){
				if($i==1){
					$data[] = '0-'.($i*1000).'米';
				}else if($i==$n-1){
					$data[] = ($i*1000).'-'.$number.'米';
				}else{
					$data[] = ($i*1000).'-'.(($i+1)*1000).'米';
				}
			}
			$this->assign('scope',$data);
			$id = $_REQUEST['id']?$_REQUEST['id']:'';
			$info = $mod->where(array('id'=>$id))->find();
			$this->assign('info', $info);
        	$this->display();
		}
	}
	public function del() {
		$mod = M('message');
		$id = $_REQUEST['id']?$_REQUEST['id']:'';
		$result = $mod->where(array('id'=>$id))->delete();
		echo $result;
	}
}
